<template>
  <div>
    <!-- 搜索 -->
    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item label="关键字:">
        <el-input placeholder="输入关键字" v-model="params.keyWord"></el-input>
      </el-form-item>
      <el-form-item label="开始时间:">
        <el-date-picker
          type="date"
          v-model="params.start"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间:">
        <el-date-picker
          type="date"
          v-model="params.end"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getCateLists" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- /搜索 -->
    <!-- 增加按钮导出excel -->
    <div class="action-btns">
      <el-button @click="cateAddShow=true" type="warning" size="mini">增加分类</el-button>
      <download-excel
        :data="cates"
        :fields="fields"
        name="商品分类"
      >
        <el-button type="danger" size="mini" icon="el-icon-download">导出</el-button>
      </download-excel>
    </div>
    <!-- 表格显示分类列表数据 -->
    <el-table
      :data="cates"
      height="300"
      style="width: 100%">
      <el-table-column
        prop="cateName"
        label="分类名称">
      </el-table-column>
      <el-table-column
        prop="enabled"
        label="显示">
         <template slot-scope="scope">
           <span>{{scope.row.enabled === 0?'显示': '隐藏'}}</span>
            <el-switch
              :value="scope.row.enabled === 0? true: false"
            />
         </template>
      </el-table-column>
      <el-table-column
        prop="icon"
        label="图标">
        <template slot-scope="scope">
          <img :src="scope.row.icon"  width="50" alt="">
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="showUpdateDrawer(scope.row.id)">编辑</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格显示分类列表数据 -->
    <!-- 分页组件 -->
    <el-pagination
      layout="prev, pager, next, jumper, sizes"
      :page-size="params.pageSize"
      :total="total"
      :current-page="params.page"
      @size-change="sizeChange"
      @current-change="currentChange"
      >
    </el-pagination>
    <!-- /分页组件 -->
    <!-- 增加分类的抽屉弹窗 -->
    <el-drawer
      title="增加分类"
      :visible.sync="cateAddShow"
     >
      <cate-add :cates="cates"/>
    </el-drawer>
    <!-- /增加分类的抽屉弹窗 -->
    <!-- 修改分类的抽屉弹窗 -->
    <el-drawer
      title="修改分类"
      :visible.sync="cateEditShow"
     >
      <cate-update :cates="cates" :id="id"/>
    </el-drawer>
    <!-- /修改分类的抽屉弹窗 -->
  </div>
</template>

<script>
import { getCateLists } from '_api'
import CateAdd from './components/CateAdd.vue'
import CateUpdate from './components/CateUpdate.vue'
export default {
  name: 'CateLists',
  data () {
    return {
      cates: [],
      // 商品分类请求的参数
      total: 0, // 总条数
      params: {
        keyWord: '',
        start: '',
        end: '',
        page: 1,
        pageSize: 10
      },
      fields: {
        // 导出excel的表头
        序号: 'id',
        分类名: 'cateName',
        分类图标地址: 'icon'
      },
      // 控制增加分类弹窗
      cateAddShow: false,
      // 控制修改分类弹窗
      cateEditShow: false,
      id: -1 // 修改分类的id
    }
  },
  methods: {
    showUpdateDrawer (id) {
      // 点击编辑按钮 显示 修改商品弹窗
      this.cateEditShow = true
      this.id = id
    },
    sizeChange (size) {
      // 分页改变一页显示多少条
      this.params.pageSize = size
      this.getCateLists()
    },
    currentChange (page) {
      // 分页组件改变页码
      this.params.page = page
      this.getCateLists()
    },
    getCateLists () {
      // 获取商品分类列表数据
      getCateLists(this.params).then(res => {
        if (res.data.code === 200) {
          this.cates = res.data.data.cateList
          this.total = res.data.data.total
          this.params = {
            keyWord: '',
            start: '',
            end: '',
            page: 1,
            pageSize: 10
          }
        }
      })
    }
  },
  created () {
    this.getCateLists()
  },
  components: {
    CateAdd,
    CateUpdate
  }
}
</script>

<style lang="scss" scoped>
.action-btns{
  margin-bottom: 10px;
  display: flex;
}
</style>
